package com.google.gwt.jquery.ui.showcase.client;

import java.util.Iterator;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.jquery.mobile.ui.Button;
import com.google.gwt.jquery.mobile.ui.HeaderBar;
import com.google.gwt.jquery.mobile.ui.Page;
import com.google.gwt.jquery.mobile.ui.SliderSwitch;
import com.google.gwt.jquery.mobile.ui.face.HasDataTheme;
import com.google.gwt.jquery.mobile.ui.util.JQueryMobile;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class Showcase implements EntryPoint, ClickHandler, ValueChangeHandler<Boolean> {

	interface MyUiBinder extends UiBinder<Page, Showcase> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	Page page = null;
	@UiField HeaderBar header;
	@UiField SliderSwitch fixedHeader;
	@UiField Button showLoader;
	@UiField Button hideLoader;
	@UiField Button switchThemeA;
	@UiField Button switchThemeB;
	@UiField Button switchThemeC;
	@UiField Button switchThemeD;
	@UiField Button switchThemeE;
	@UiField Button projectSite;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		JQueryMobile.init();

		// create add the page
		page = uiBinder.createAndBindUi(this);
		showLoader.addClickHandler(this);
		hideLoader.addClickHandler(this);
		switchThemeA.addClickHandler(this);
		switchThemeB.addClickHandler(this);
		switchThemeC.addClickHandler(this);
		switchThemeD.addClickHandler(this);
		switchThemeE.addClickHandler(this);
		fixedHeader.addValueChangeHandler(this);
		projectSite.addClickHandler(this);
		RootPanel.get().add(page);
	}

	private void switchTheme(String theme) {
		switchTheme(page, theme);
	}

	private void switchTheme(Widget w, String theme) {
		if (w instanceof HasDataTheme) {
			if (null == w.getStyleName() || w.getStyleName().indexOf("sticky-theme") < 0) {
				((HasDataTheme) w).setDataTheme(theme);
			}
		}
		if (w instanceof HasWidgets) {
			Iterator<Widget> iter = ((HasWidgets) w).iterator();
			while (iter.hasNext()) {
				switchTheme(iter.next(), theme);
			}
		}
	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource().equals(showLoader))
			JQueryMobile.showLoader();
		else if (event.getSource().equals(hideLoader))
			JQueryMobile.hideLoader();
		else if (event.getSource().equals(switchThemeA))
			switchTheme("a");
		else if (event.getSource().equals(switchThemeB))
			switchTheme("b");
		else if (event.getSource().equals(switchThemeC))
			switchTheme("c");
		else if (event.getSource().equals(switchThemeD))
			switchTheme("d");
		else if (event.getSource().equals(switchThemeE))
			switchTheme("e");
		else if (event.getSource().equals(projectSite))
			Window.open("http://code.google.com/p/gwt-jquery-ui/", "project", null);
	}

	@Override
	public void onValueChange(ValueChangeEvent<Boolean> event) {
		if (event.getSource().equals(fixedHeader)) {
			header.setFixed(event.getValue());
		}
	}
}